//
//  JHButton.m
//  UIHomeWork_07
//
//  Created by tusm on 16/7/23.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "JHButton.h"
#define kTitleRatio 0.2
@implementation JHButton

+ (id)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame title:(NSString *)title image:(UIImage *)image handler:(tapHandler)handler{

    JHButton *button = [super buttonWithType:buttonType];
    button.frame = frame;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;    //文字居中
    button.titleLabel.font = [UIFont systemFontOfSize:28];  //文字大小
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    button.imageView.contentMode = UIViewContentModeCenter; //图片的内容模式
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    button.handler = handler;
    [button addTarget:button action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

#pragma mark - handleButton
- (void)handleButton:(UIButton *)sender{

    if (self.handler) {
        self.handler(sender);
    }

}

#pragma mark - 调整内部ImageView的frame --
- (CGRect)imageRectForContentRect:(CGRect)contentRect{

    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageWidth = contentRect.size.width;
    CGFloat imageHeight = contentRect.size.height * (1 - kTitleRatio);
    return CGRectMake(imageX, imageY, imageWidth, imageHeight);

}
#pragma mark - 调整内部UIlable的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{

    CGFloat titleX = 0;
    CGFloat titleHeight = contentRect.size.height * kTitleRatio;
    CGFloat titleY = contentRect.size.height - titleHeight + 10;
    CGFloat titleWidth = contentRect.size.width;
    return CGRectMake(titleX, titleY, titleWidth, titleHeight);
    
}

@end
